﻿using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Data.Entity;
using System.Web.Mvc;
using TS2015.Models;

namespace TS2015.Controllers
{
    public class CommonController : Controller
    {
        TS2015Context db = new TS2015Context();
        // GET: Common
        public PartialViewResult Banner()
        {
            return PartialView();
        }

        public PartialViewResult Menu()
        {
            return PartialView();
        }
        public PartialViewResult NavbarLeft()
        {
            return PartialView();
        }
        public PartialViewResult Slide()
        {
            return PartialView();
        }
        public PartialViewResult Footer()
        {
            return PartialView();
        }
        public ActionResult LockScreen()
        {
            return View();
        }
        public ActionResult Login(int? id)
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(string Username, string Password)
        {
            ViewBag.Username = Username + " " + Password;
            return RedirectToRoute("Admin", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
            //return View();

        }
        
        public ActionResult PhieuDangKy(string MaTS)
        {
            TbThiSinh ThiSinh = new TbThiSinh();
            if (ModelState.IsValid)
            {
                if (MaTS == null)
                {
                    //Database.SetInitializer<TS2015Context>(null);
                    ViewBag.ChuyenNganh = new SelectList(db.TbChuyenNganhs.ToList(), "MaChuyenNganh", "TenChuyenNganh");
                    ViewBag.Truong = new SelectList(db.TbTruongs.ToList(), "MaTruong", "TenTruong");
                    ViewBag.KhuVuc = new SelectList(db.TbKhuVucs.ToList(), "MaKhuVuc", "TenKhuVuc");
                    ViewBag.XepLoaiHS = new SelectList(db.TbXepLoaiHS.ToList(), "MaXepLoaiHS", "TenXepLoaiHS");
                    ViewBag.DanToc = new SelectList(db.TbDanTocs.ToList(), "MaDanToc", "TenDanToc");
                    ViewBag.DoiTuongUuTien = new SelectList(db.TbDoiTuongUuTiens.ToList(), "MaDoiTuong", "TenDoiTuong");
                    ViewBag.KhoiThi = new SelectList(db.TbKhoiThis.ToList(), "MaKhoiThi", "TenKhoiThi");
                    ViewBag.HeDaoTao = new SelectList(db.TbHeDaoTaos.ToList(), "MaHeDaoTao", "TenHeDaoTao");
                }
                else
                {
                    ThiSinh = db.TbThiSinhs.First(e => e.MaTS == MaTS);

                    ViewBag.ChuyenNganh = new SelectList(db.TbChuyenNganhs.ToList(), "MaChuyenNganh", "TenChuyenNganh",ThiSinh.MaChuyenNganh);
                    ViewBag.Truong = new SelectList(db.TbTruongs.ToList(), "MaTruong", "TenTruong", ThiSinh.MaTruong);
                    ViewBag.KhuVuc = new SelectList(db.TbKhuVucs.ToList(), "MaKhuVuc", "TenKhuVuc", ThiSinh.MaKhuVuc);
                    ViewBag.XepLoaiHS = new SelectList(db.TbXepLoaiHS.ToList(), "MaXepLoaiHS", "TenXepLoaiHS", ThiSinh.MaXepLoaiHS);
                    ViewBag.DanToc = new SelectList(db.TbDanTocs.ToList(), "MaDanToc", "TenDanToc", ThiSinh.MaDanToc);
                    ViewBag.DoiTuongUuTien = new SelectList(db.TbDoiTuongUuTiens.ToList(), "MaDoiTuong", "TenDoiTuong", ThiSinh.MaDoiTuong);
                    ViewBag.KhoiThi = new SelectList(db.TbKhoiThis.ToList(), "MaKhoiThi", "TenKhoiThi", ThiSinh.MaKhoiThi);
                    ViewBag.HeDaoTao = new SelectList(db.TbHeDaoTaos.ToList(), "MaHeDaoTao", "TenHeDaoTao", ThiSinh.MaHeDaoTao);
                }
            }
            
            
            if (Request.IsAjaxRequest() || MaTS!=null)
                return PartialView(ThiSinh);

                return View(ThiSinh);
        }

        public ActionResult PhieuHoanThanh()
        {
            return View("PhieuHoanThanh");
        }
        public ActionResult Profile()
        {
            return View("Profile");
        }

        [ChildActionOnly]
        public ActionResult TinNoiBat([Range(0, int.MaxValue)]int? SoLuong)
        {
            Database.SetInitializer<TS2015Context>(null);
            int _SoLuong = SoLuong ?? -1;

            var data = db.TbTinTucs.Where(e => e.NoiBat == true);

             if (_SoLuong > -1)
                return PartialView("_TinNoiBat",data.Take(_SoLuong));
            return PartialView("_TinNoiBat", data);
        }

        [ChildActionOnly]
        public ActionResult TinTheoTheLoai(int MaTheLoai, [Range(0, int.MaxValue)]int? SoLuong)
        {
            Database.SetInitializer<TS2015Context>(null);
            int _SoLuong = SoLuong ?? -1;

            var data=db.TbTinTucs.Include(e => e.TbTheLoai).Where(e => e.MaTheLoai == MaTheLoai);

            if (data == null)
                return HttpNotFound();

            if (_SoLuong > -1)
                return PartialView("_TinTheoTheLoai",data.Take(_SoLuong));

            return PartialView("_TinTheoTheLoai", data);
        }
        
        public ActionResult ChiTiet(int id, string seoName)
        {
            try
            {
                var data = db.TbTinTucs.First(e => e.MaTinTuc == id);

                if (data == null)
                    return HttpNotFound();

                if (seoName != TsServices.SeoName(data.TieuDe))
                    return RedirectToRoute("ChiTiet", new { controller = "Common", action = "ChiTiet", id = id, seoName = TsServices.SeoName(data.TieuDe) });

                return View(data);
            }
            catch 
            {
                return HttpNotFound();
            }
           
        }
    }
}

